package com.ry;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ry.model.CarSelectModel;
import com.ry.util.CommonConstants;
import com.ry.util.timepick.R;

public class CityAndModelActivity extends Activity {
	private ListView lstCarModel;
	private CarModelAdapter carModelAdapter;
	private Button btnBack;
	private TextView txtLocation;
	private String type;

	private List<CarSelectModel> carModelList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.city_and_model);
		init();
	}

	private void init() {
		type = getIntent().getStringExtra("type");
		if (type == null) {
			return;
		}
		lstCarModel = (ListView) findViewById(R.city_and_model.lst_car_model);
		btnBack = (Button) findViewById(R.city_and_model.btn_back);

		carModelList = new ArrayList<CarSelectModel>();
		CarSelectModel model0 = new CarSelectModel();
		model0.carModel = "经济车型";
		model0.carModelDetail = "锋苑|伊兰特|宝来|卡罗拉等同级车型";
		model0.price = "168";
		model0.priceOverKilometre = "3";
		model0.priceOverTime = "15";
		carModelList.add(model0);

		CarSelectModel model1 = new CarSelectModel();
		model1.carModel = "商务车型";
		model1.carModelDetail = "别克GL8|陆尊等同级车型";
		model1.price = "368";
		model1.priceOverKilometre = "5";
		model1.priceOverTime = "25";
		carModelList.add(model1);

		CarSelectModel model2 = new CarSelectModel();
		model2.carModel = "豪华车型";
		model2.carModelDetail = "奥迪A6L等同级车型";
		model2.price = "488";
		model2.priceOverKilometre = "5";
		model2.priceOverTime = "30";
		carModelList.add(model2);

		CarSelectModel model3 = new CarSelectModel();
		model3.carModel = "舒适车型";
		model3.carModelDetail = "帕萨特|天籁|本田雅阁|迈腾等同级车型";
		model3.price = "188";
		model3.priceOverKilometre = "4";
		model3.priceOverTime = "20";
		carModelList.add(model3);

		LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View viewHeader = mInflater.inflate(R.layout.lst_car_model_header, null);
		View viewFooter = mInflater.inflate(R.layout.lst_car_model_footer, null);
		RelativeLayout rlytLocation = (RelativeLayout) viewHeader.findViewById(R.lst_car_model_header.rlyt_location);
		txtLocation = (TextView) viewHeader.findViewById(R.lst_car_model_header.txt_location);
		rlytLocation.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CityAndModelActivity.this, CitySelectActivity.class);
				startActivityForResult(intent, 0);
			}
		});
		TextView txtInstructionPriceDetail = (TextView) viewFooter
				.findViewById(R.lst_car_model_footer.txt_instruction_price_detail);
		txtInstructionPriceDetail.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CityAndModelActivity.this, ValuationInstructionActivity.class);
				startActivity(intent);
			}
		});
		lstCarModel.addHeaderView(viewHeader);
		lstCarModel.addFooterView(viewFooter);
		carModelAdapter = new CarModelAdapter();
		lstCarModel.setAdapter(carModelAdapter);

		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CityAndModelActivity.this.finish();
			}
		});

		lstCarModel.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (CommonConstants.DAILY_RENT.equals(type) || CommonConstants.HOUR_RENT.equals(type)
						|| CommonConstants.CUSTOM_ROUTE.equals(type)) {
					Intent intent = new Intent(CityAndModelActivity.this, UseCarInformationActivity.class);
					startActivity(intent);
				} else if (CommonConstants.AIRPORT_PICK_UP.equals(type)
						|| CommonConstants.AIRPORT_DROP_OFF.equals(type) || CommonConstants.TRAIN_PICK_UP.equals(type)
						|| CommonConstants.TRAIN_DROP_OFF.equals(type)) {
					Intent intent = new Intent(CityAndModelActivity.this, FlightOrTrainInformationActivity.class);
					intent.putExtra("type", type);
					startActivity(intent);
				}
			}
		});
	}

	public class CarModelAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return carModelList.size();
		}

		@Override
		public Object getItem(int position) {
			return carModelList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.car_model_item, null);
				holder = new ViewHolder();
				holder.txtCarModel = (TextView) convertView.findViewById(R.car_model_item.txt_car_model);
				holder.txtCarModelDetail = (TextView) convertView.findViewById(R.car_model_item.txt_car_model_detail);
				holder.txtPrice = (TextView) convertView.findViewById(R.car_model_item.txt_price);
				holder.txtPriceOverKilometre = (TextView) convertView
						.findViewById(R.car_model_item.txt_price_over_kilometre);
				holder.txtPriceOverTime = (TextView) convertView.findViewById(R.car_model_item.txt_price_over_time);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.txtCarModel.setText(carModelList.get(position).carModel);
			holder.txtCarModelDetail.setText(carModelList.get(position).carModelDetail);
			holder.txtPrice.setText(carModelList.get(position).price);
			holder.txtPriceOverKilometre.setText(carModelList.get(position).priceOverKilometre);
			holder.txtPriceOverTime.setText(carModelList.get(position).priceOverTime);
			return convertView;
		}

	}

	public static class ViewHolder {
		public TextView txtCarModel;
		public TextView txtCarModelDetail;
		public TextView txtPrice;
		public TextView txtPriceOverKilometre;
		public TextView txtPriceOverTime;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 0 && resultCode == RESULT_OK){
			String cityName = data.getStringExtra("city");
			txtLocation.setText(cityName);
		}
	}
	
	
}
